/**
 * BanBu_PhotoMoveListViewController
 * @description 本文件提供转移照片选择转移相册界面/上传照片选择相册界面
 * @package
 * @author 		yinlinlin
 * @copyright
 * @version 		1.0
 * @description 本文件提供转移照片选择转移相册界面/上传照片选择相册界面
 */

#import "BanBu_PhotoMoveListViewController.h"
#import "BanBu_PhotoMoveListCell.h"
#import "UIImageView+WebCache.h"
#import "BanBu_NavigationController.h"

#import "AppCommunicationManager.h"
#import "AppDataManager.h"
#import "UIViewController+Animation.h"
#import "BanBu_ImageView.h"
#import "BanBu_NavButton.h"
#import "BanBu_ThemeManager.h"
#import "SDNCreateAlbumsViewController.h"
@interface BanBu_PhotoMoveListViewController ()
{
    NSMutableArray * _albumlist;
}
@end

@implementation BanBu_PhotoMoveListViewController

- (void)dealloc
{
    self.selectAlnumCallBack = nil;
    [_albumlist release];
    _albumlist = nil;
    [super dealloc];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"myalbumTitle", 我的相册);
    _albumlist = [[NSMutableArray alloc]init];
    [self getPhotoAlbumList];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 获取相册列表
/**
 * @函数名称：getPhotoAlbumList
 * @函数描述：获取用户所有相册列表
 * @输入参数：void
 * @输出参数：获取用户所有相册列表
 * @返回值：void
 */
-(void)getPhotoAlbumList
{
    [TKLoadingView showTkloadingAddedTo:self.view title:NSLocalizedString(@"loadingNotice", nil) activityAnimated:YES];
    NSDictionary *parDic = [NSDictionary dictionaryWithObjectsAndKeys:MyAppDataManager.useruid,@"userid",nil];
    [AppComManager getBanBuAlbumData:BanBu_v2_Get_Albumlist_Byuser par:parDic delegate:self];
}

#pragma  mark - banbuRequestDidFinished
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    NSLog(@"%@",resDic);
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:NO afterShow:0];
    [TKLoadingView dismissTkFromView:self.view animated:NO afterShow:0];
    
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:1];
        
    }
    else if([AppComManager respondsDic:resDic isFunctionData:BanBu_v2_Get_Albumlist_Byuser])
    {
        //        NSLog(@"%@",resDic);
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            NSArray *albumList = [resDic objectForKey:@"albumlist"];
            NSLog(@"%@",albumList);
            [_albumlist removeAllObjects];
            for (NSDictionary * albumDict in albumList)
            {
                BanBu_AlbumModel * album_model = [[BanBu_AlbumModel alloc]init];
                album_model.album_id = [albumDict valueForKey:@"albumid"];
                NSDictionary * content = [albumDict valueForKey:@"content"];
                //系统相册
                if ([[albumDict valueForKey:@"is_sys"] boolValue])
                {
                    album_model.album_title = [MyAppDataManager IsInternationalLanguage:[albumDict valueForKey:@"albumname"]];
                    NSLog(@"%@",album_model.album_title);
                    album_model.album_type = ALBUM_SYSTEM;
                }
                else//用户自定义相册
                {
                    album_model.album_title = [albumDict valueForKey:@"albumname"];
                    album_model.album_type = ALBUM_PERSONAL;
                    if ([content isKindOfClass:[NSDictionary class]])
                    {
                        NSLog(@"%d",[[content valueForKey:@"stringof"] integerValue]);
                        album_model.personalbum_type = [[content valueForKey:@"stringof"] integerValue];
                        album_model.question = [content valueForKey:@"byquestion"];
                        album_model.answer = [content valueForKey:@"answerof"];
                        album_model.opengroup = [content valueForKey:@"opengroup"];
                        album_model.hidegroup = [content valueForKey:@"hidegroup"];
                    }
                }
                if ([content isKindOfClass:[NSDictionary class]])
                {
                    album_model.image_url = [content valueForKey:@"facefile"];
                    album_model.faceIdStr = [content valueForKey:@"facejoin"];
                }
                
                album_model.numCount = [[albumDict valueForKey:@"countof"] integerValue];
                [_albumlist addObject:album_model];
                [album_model release];
            }
            [self.tableView reloadData];
            
        }
    }
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _albumlist.count + 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentify = @"BanBu_PhotoMoveListCell";
    BanBu_PhotoMoveListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell)
    {
        cell = [[[BanBu_PhotoMoveListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.row == 0)
    {
        [cell updatePhotoMoveListCell:nil];
    }
    else
    {
        [cell updatePhotoMoveListCell:[_albumlist objectAtIndex:indexPath.row - 1]];
    }
    // Configure the cell...
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //新建相册
    if (indexPath.row == 0)
    {
        SDNCreateAlbumsViewController *nestedNav = [[SDNCreateAlbumsViewController alloc] init];
        __block BanBu_PhotoMoveListViewController * bself = self;
        //创建相册成功，重新请求数据
        [nestedNav setCreateSuccessCallBack:^(BanBu_AlbumModel * album_Model){
            if (_selectAlnumCallBack)
            {
                _selectAlnumCallBack(album_Model.album_id,album_Model.album_title);
            }
            [bself performSelector:@selector(popTolaseView) withObject:nil afterDelay:1.0];
        }];
        [self.navigationController pushViewController:nestedNav animated:YES];
        [nestedNav release];
    }
    else
    {
        BanBu_AlbumModel * album_model = [_albumlist objectAtIndex:indexPath.row - 1];
        if (_selectAlnumCallBack)
        {
            _selectAlnumCallBack(album_model.album_id,album_model.album_title);
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)popTolaseView
{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
